<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>累积和示例</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f0f0f0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .container {
            background-color: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            text-align: center;
            width: 350px;
        }

        h1 {
            font-size: 24px;
            color: #333;
        }

        input {
            width: 100%;
            padding: 10px;
            font-size: 16px;
            margin-bottom: 10px;
            border-radius: 5px;
            border: 1px solid #ddd;
            box-sizing: border-box;
        }

        button {
            width: 100%;
            padding: 12px;
            font-size: 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #45a049;
        }

        .output {
            margin-top: 20px;
            padding: 10px;
            background-color: #e8f5e9;
            border-radius: 5px;
            color: #388e3c;
            font-size: 18px;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>累积和示例</h1>
        <input type="text" id="inputNumbers" placeholder="请输入以逗号分隔的数字" />
        <button onclick="calculateAccumulation()">计算累积和</button>
        <div class="output" id="output"></div>
    </div>

    <script>
        // 累积和的函数
        const accumulate = (...args) =>
            args.reduce((acc, n) => [...acc, n + +acc.slice(-1)], []);

        // 计算累积和的函数
        function calculateAccumulation() {
            const input = document.getElementById('inputNumbers').value.trim();
            if (input === "") {
                document.getElementById('output').innerText = "请输入一些数字！";
                return;
            }
            const numbers = input.split(',').map(num => parseFloat(num.trim())).filter(num => !isNaN(num));

            if (numbers.length === 0) {
                document.getElementById('output').innerText = "请输入有效的数字！";
                return;
            }

            // 使用累积和函数计算结果
            const result = accumulate(...numbers);

            // 显示累积和的结果
            document.getElementById('output').innerText = "累积和结果: " + result.join(', ');
        }
    </script>

</body>

</html>